// nodejs核心模块，直接使用
const os = require("os");
// cpu核数
const threads = os.cpus().length;

const path = require("path");
const ESLintPlugin = require("eslint-webpack-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const TerserWebpackPlugin = require("terser-webpack-plugin");





function getStyleLoader(pre) {
  return [
    MiniCssExtractPlugin.loader,
    "css-loader",
    {
      loader: "postcss-loader",
      options: {
        postcssOptions: {
          plugins: [
            "postcss-preset-env", // 能解决大多数样式兼容性问题
          ],
        },
      },
    },
    pre,
  ].filter(Boolean);
}

module.exports = {
  entry: "./src/main.js",
  output: {
    path: path.resolve(__dirname, "../dist"),
    filename: "static/js/main.js",
    clean: true
  },
  module: {
    rules: [
      {
        oneOf: [
          {
            test: /\.css$/,
            use: getStyleLoader()
          },
          {
            test: /\.less$/,
            use: getStyleLoader("less-loader")
          },
          {
            test: /\.s[ac]ss$/,
            use: getStyleLoader("sass-loader")
          },
          {
            test: /\.styl$/,
            use: getStyleLoader("stylus-loader")
          },
          {
            test: /\.(png|jpe?g|gif|webp|svg)$/,
            type: "asset",
            parser: {
              dataUrlCondition: {
                maxSize: 10 * 1024
              }
            },
            generator: {
              filename: "static/images/[hash:10][ext][query]"
            }
          },
          {
            test: /\.(ttf|woff2?|mp3|mp4|avi)$/,
            type: "asset/resource",
            generator: {
              filename: "static/media/[hash:10][ext][query]"
            }
          },
          {
            test: /\.js$/,
            exclude: /node_modules/,
            // include:path.resolve(__dirname,"../src"),
            use: [
              {
                loader: "thread-loader",
                options: {
                  works: threads
                }
              },
              {
                loader: "babel-loader",
                options: {
                  //   presets: ['@babel/preset-env']
                  cacheDirectory: true,//开启cache缓存
                  cacheCompression: false//关闭缓存文件压缩
                }
              }
            ]

          }
        ]
      }

    ]
  },
  plugins: [
    new ESLintPlugin({
      context: path.resolve(__dirname, "../src"),
      exclude: "node_modules",
      cache: true,
      cacheLocation: path.resolve(__dirname, "../node_modules/.cache/eslint"),
      threads
    }),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, "../public/index.html")
    }),
    new MiniCssExtractPlugin({
      filename: "static/css/index.css"
    }),
    // new CssMinimizerPlugin(),
    // new TerserWebpackPlugin({
    //   parallel:threads
    // })
  ],
  optimization: {
    //压缩的操作
    minimizer: [
      new CssMinimizerPlugin(),
      new TerserWebpackPlugin({
        parallel: threads
      })
    ]
  },
  devtool: "source-map",
  mode: "production"
}